package com.hp.goods_details_service.service;

import com.hp.common_api.entity.*;
import com.hp.goods_details_service.feign.GoodsFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class GoodsDetailsService {

    @Autowired
    private GoodsFeignClient goodsFeignClient;

    /**
     * 通过商品id查询所有详情页面需要的数据
     * @param spuId
     * @return
     */
    public Map<String,Object> getGoodsDetailsMap(Long spuId){
        //查询商品
        Spu spu = goodsFeignClient.getSpuById(spuId).getBody();
        if(spu == null){
            log.info("商品"+spuId+"不存在");
            return null;
        }
        //查询分类
        Category c1 = goodsFeignClient.getCategoryById(spu.getCid1()).getBody();
        Category c2 = goodsFeignClient.getCategoryById(spu.getCid2()).getBody();
        Category c3 = goodsFeignClient.getCategoryById(spu.getCid3()).getBody();
        List<Category> categories = Arrays.asList(c1,c2,c3);
        //查询品牌
        Brand brand = goodsFeignClient.getBrandById(spu.getBrandId()).getBody();
        //查询sku
        List<Sku> skus = goodsFeignClient.getSkusBySpuId(spuId).getBody();
        //查询商品详情
        SpuDetail spuDetail = goodsFeignClient.getSpuDetailBySpuId(spuId).getBody();
        List<SpecGroup> groups = goodsFeignClient.getSpecGroupsByCategoryId(spu.getCid3()).getBody();
        //查询分类下的规格，key：id value: name
        List<SpecParam> specParams = goodsFeignClient.getSpecParamsByCategoryId(spu.getCid3()).getBody();
        Map<Long,String> paramMap = new HashMap<>();
        specParams.forEach(param->{
            paramMap.put(param.getId(),param.getName());
        });
        Map<String,Object> map = new HashMap<>();
        map.put("spu",spu);
        map.put("categories",categories);
        map.put("brand",brand);
        map.put("skus",skus);
        map.put("spuDetail",spuDetail);
        map.put("groups",groups);
        map.put("paramMap",paramMap);
        return map;
    }
}
